const {createAddr,addressFindall,updateAddress,addrRemove,updateDefaultAddr} = require('../service/addr.service')
class addrController {
    async create(ctx){
        const user_id = ctx.state.user.id
        const {consignee,phone,address} = ctx.request.body

        const res = await createAddr({user_id,consignee,phone,address})
        ctx.body = {
            code:0,
            message:'添加地址成功',
            result:res
        }
     }
    async getAddress(ctx){
        const user_id = ctx.state.user.id

        const res = await addressFindall(user_id)
        ctx.body={
            code:0,
            message:'查询所有地址成功',
            result:res
        }
    }
    async update(ctx){
        const id = ctx.request.params.id

        const data = ctx.request.body
        const res = await updateAddress(id,data)
        
        ctx.body = {
            code:0,
            message:'更新地址成功',
            result:res
        }
    }
    async remove(ctx){
        const id = ctx.request.params.id
        const res = await addrRemove(id)
        ctx.body ={
            code:0,
            message:'删除地址成功',
            result:res
        }
    }
    async updateDefault(ctx){
        const user_id = ctx.state.user.id
        const id = ctx.request.params.id
        const res = await updateDefaultAddr(user_id,id)
        ctx.body = {
            code:0,
            message:'修改默认地址成功',
            result:res
        }
    }
}

module.exports = new addrController()